const koa = require("koa");
const router = require("koa-router")();
const bodyParser = require("koa-bodyparser");
const cors = require("koa2-cors");
const app = new koa();
router.post("/login",async ctx=>{
    /* username=zz,psd=123 */
    var {username,psd} = ctx.request.body;
    console.log(username,psd);
    if(username=="zz" && psd == "123"){
        // 设置缓存,并且设置前端可读
        ctx.cookies.set('loginAuth',true,{
            httpOnly:false //前端可读
        });
        ctx.body= {
            code:200,
            auth:true,
            msg:"登录成功"
        }
    }else{
        ctx.body = {
            code:400,
            msg:"用户名或密码错误"
        }
    }
    
})
app.use(cors({
    // origin:"http://192.168.4.16:8081", //写死的地址
    origin:ctx=>{
        console.log(ctx.href);
        return ctx.header.origin;
    },
    credentials:true
}));
app.use(bodyParser());
app.use(router.routes());
app.listen(9000);
